﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.ComponentModel;
//using System.Reflection;
using Microsoft.Xna.Framework;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Content;

namespace FelixGameNS
{
    [Serializable]
    public class AppSettings
    {

        private static AppSettings _instance = null;
        public bool settingsFullscreen { get; set; }
        private string fileName = "AppSettings.xml";
        private string content = "Content";

        string assemblyLocation = Assembly.GetExecutingAssembly().Location;
        string fbxPath, levelsPath;

        [XmlIgnore]
        public bool soundHW { get; set; }


        public string ContentRootDirectory { get; set; }

        public string path { get; set; }
        [XmlIgnore]
        public ContentBuilder contentBuilder{ get; set; }
        [XmlIgnore]
        public ContentManager contentManager{ get; set; }

        public SerializableDictionary<int, Level> Levels = new SerializableDictionary<int, Level>();

        public static AppSettings instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AppSettings();

                }

                return _instance;
            }
        }


        public void ContentBuild()
        {
            //Cursor = Cursors.WaitCursor;
            Console.WriteLine("Modellek feldolgozása elkezdődött...");
            contentManager.Unload();

            contentBuilder.Clear();

            string ss = Path.GetFullPath(fbxPath + "/");
            foreach (KeyValuePair<int, Level> pair in Levels)
            {
                foreach (string file in pair.Value.files)
                {
                    string s2 = Path.GetFileNameWithoutExtension(fileName);
                    contentBuilder.Add(ss + pair.Value.name+"/"+file+".fbx", pair.Value.name + "_" + file, null, "ModelProcessor"); 
                }

            }
 
            string buildError = contentBuilder.Build();
            Console.WriteLine("Modellek feldolgozása befejeződött...");
            //Cursor = Cursors.Arrow;

            if (!string.IsNullOrEmpty(buildError))
            {
                MessageBox.Show(buildError, "Error");
            }
        }

        /// <summary>
        /// adatok HDD-re írása
        /// </summary>
        public static void adatKiiras()
        {
            try
            {
                XmlSerializer serializer;
                TextWriter WriteFileStream;
                serializer = new XmlSerializer(typeof(AppSettings));
                WriteFileStream = new StreamWriter(instance.path + instance.fileName, false);
                serializer.Serialize(WriteFileStream, instance);
                WriteFileStream.Close();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        /// <summary>
        ///  HDD-ről adatok olvasása
        /// </summary>
        public void adatfelolvasas()
        {
            ContentRootDirectory = content;
            path = "data\\";
            levelsPath = Path.Combine(assemblyLocation, "../../../../../config/Levels.xml");
            fbxPath = Path.Combine(assemblyLocation, "../../../../../fbx");
            try
            {
                Levels.Clear();
                XmlSerializer serializer;
                TextReader ReadFileStream;
                serializer = new XmlSerializer(typeof(AppSettings));
                ReadFileStream = new StreamReader(path + fileName, false);
                AppSettings a = (AppSettings)serializer.Deserialize(ReadFileStream);
                instance.Levels = a.Levels;
                Console.WriteLine("Alkalmazás beállítások beolvasása");
                ReadFileStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            foreach (KeyValuePair<int, Level> pair in Levels)
            {
                string ss = Path.GetFullPath(fbxPath + "/" + pair.Value.foldername);
                if (Directory.Exists(ss))
                {
                    string[] fileEntries = Directory.GetFiles(ss);
                    pair.Value.files.Clear();
                    foreach (string fileName in fileEntries)
                    {
                        string s2 = Path.GetFileNameWithoutExtension(fileName);
                        //contentBuilder.Add(fileName, pair.Value.name + "_" + Path.GetFileNameWithoutExtension(fileName), null, "ModelProcessor");
                        pair.Value.files.Add(Path.GetFileNameWithoutExtension(fileName));
                    }
                }

                else
                {
                    Console.WriteLine("{0} is not a valid file or directory.", pair.Value.foldername);
                }

            }

            contentBuilder = new ContentBuilder();

            //Level l = new Level();
            //l.name = "level1";
            //l.foldername = "level1";
            //Levels.Add("1", l);
            //l = new Level();
            //l.name = "level2";
            //l.foldername = "level2";
            //Levels.Add("2", l);
        }

       

        public string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }
    }
}
